* Firm organization with multiple establishments
* Section V: Table VII, Appendix Tables D.3-D.4, D.6-D.11, D.15-D.16, D.18-D.24 
* Top panel: Directly affected establishments

clear all 
set matsize 2000
set more off

capture log close
log using log/20a_train_layer-firm_est-direct.log, replace

/*
Contents: Results for directly affected establishments

	Table VII: Lower travel times affect all units of ME firms
	Table D.3: summary statistics

	Robustness to alternative control groups, strategic location
	Table D.4/Figure VII:
		a.	Firms with one establishment
		b.	Excl. indirectly affected establishments
		c.	Entry before 2000, all firms/firms with at least two establishments
		d.	Entry before 1995, all firms/firms with at least two establishments

	Robustness to alternative weighting, matching, statistical inference
		Table D.6: Table VII unweighted
		Table D.7: Table D.4 unweighted
		Table D.8: Table VII without matching
		Table D.9: Table D.4 without matching
		Table D.10: Table VII SE clustered at firm-county-level
		Table D.11: Table D.4 SE clustered at firm-county-level

	Robustness to alternative definitions of the main variables
		Table D.15: Table VII with additional outcome variables (firms with at least two establishments)
				log total number of employees; 
				average log wages of all employees;
				managerial span, defined as ratio of the number of employees over number of employees in the top layer (as in Friedrich 2020); 
				share of managerial occupations in wage sum in percent, where managerial occupations are determined by layer;
				ed. 0: education of non-managerial employees;
				ed. all: education of all employees;
				ex. 0: average log experience (= # of days in establishment/HQ) of non-managerial employees;
				ex. all: average log experience of all employees
		Table D.16: Table VII with stricter and broader treatment definition
		Table D.18: Table VII with Bundesland x year FE

	Robustness to alternative sample restrictions
		Table D.19: Table VII without route HH-Berlin
		Table D.20: Table VII without largest units per county
		Table D.21: Table VII without stations on route
		Table D.22: Table VII w/o moving establishments
		Table D.23: Table VII with only always connected stations	

	Helping cost channel
		Table D.24: sample split by predictability
	
	Note: For coding reasons, 
	- the results for Tables D.3, D.6, D.10, D.15 are generated along with those for Table VII;
	- the results for Tables D.7, D.11 are generated along with those for Table D.4/Figure VII.
*/

use data/train_analysis_firm.dta, clear
desc

//	For D.18
cap drop bula
gen bula = int(ao_kreis / 1000)
gen double bula_year = bula * 10000 + jahr

//	For D.4: Create dummy for indirectly affected establishment with entry year before treatment year
gen aux_jahr = jahr if firm_faster1 == 1
bys untid: egen treat_year = min(aux_jahr)
drop aux_jahr
gen indirect1 = (ever_faster1 == 0 & firm_ever_faster1 == 1)
replace indirect1 = 0 if entryyear >= treat_year & indirect1 == 1

//	For D.16: Create information on broader treatment definition
gen aux_jahr = jahr if faster1 == 1
bys betnr: egen treat_year_est = min(aux_jahr)
drop aux_jahr
cap drop flg_est
egen flg_est = tag(betnr)
tab treat_year_est if flg_est == 1

preserve

keep untid ao_kreis ever_faster1 treat_year_est
rename ao_kreis ptnr_cty
rename treat_year_est ptnr_treat_year
sort untid ptnr_cty
count
duplicates drop
count
save data/firm_county.dta, replace

restore

preserve

keep untid ao_kreis
duplicates drop
sort untid ao_kreis
count
joinby untid using data/firm_county.dta
drop if ao_kreis == ptnr_cty 
duplicates report untid ao_kreis ptnr_cty

count

rename ptnr_cty hq_kreis
sort hq_kreis ao_kreis
merge m:1 hq_kreis ao_kreis using data/Distanzen_v2014.dta
drop if _merge == 2
replace distance_all = . if _merge == 1
drop _merge
rename hq_kreis ptnr_cty
keep untid ao_kreis ptnr_cty distance_all ever_faster1 ptnr_treat_year

bys untid ao_kreis: egen min_dist = min(distance_all)
keep if distance_all == min_dist
count if min_dist == .
keep untid ao_kreis ptnr_cty min_dist ever_faster1 ptnr_treat_year
rename ever_faster1 ptnr_ever_faster1

describe
duplicates report untid ao_kreis 

drop if min_dist == .
// drop if establishment not treated but other establishment with same distance would be treated
bys untid ao_kreis: egen max_treat = max(ptnr_ever_faster1)
drop if ptnr_ever_faster1 == 0 & max_treat == 1

duplicates report untid ao_kreis
duplicates drop untid ao_kreis, force
save data/est_shortest_distance.dta, replace

restore

********************************************************************************
***	TREATED ESTABLISHMENTS	****************************************************
********************************************************************************
 
********************************************************************************
***	BASELINE REGRESSION
*** TABLE VII, full sample, Table D.3, D.6, D.10

{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

qui reghdfe ln_prdt_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)

********************************************************************************
*** Table D.3: Descriptive statistics
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet faster1 {
	tabstat `outcome' if e(sample), by(jahr) s(n mean sd p5 p10 q p90 p95)
}

********************************************************************************
*** TABLE VII: Lower travel times affect all units of ME firms, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.6: Table VII without weights, top left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.10: Table VII SE clustered at firm x county level, top left
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

*** Not reported: additional outcome variables (only reported for firms with two establishments)
local j = 1
foreach outcome in ln_empl_bet avg_lnw_bet ln_span_bet_bf shr_mgmt_w_bet avg_d_educ_bot_bet avg_d_educ_bet avg_tage_bet_bot_bet avg_tage_bet_bet {

	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .
	
	eststo vcew`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcew`j'
		qui estadd scalar count_kreis = e(K2): vcew`j'
	local j = `j' + 1
}

esttab vcew1 vcew2 vcew3 vcew4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcew5 vcew6 vcew7 vcew8, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	BASELINE REGRESSION FOR FIRMS WITH MORE THAN ONE ESTABLISHMENT
*** Table VII, Table D.6, D.10, D.15

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet  {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
***	TABLE VII: Lower travel times affect all units of ME firm, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label	
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.6: Table VII without weights, top right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
***	Table D.10: Table VII SE clustered at firm x county level, top right
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

*** Additional outcome variables
local j = 1
foreach outcome in ln_empl_bet avg_lnw_bet ln_span_bet_bf shr_mgmt_w_bet avg_d_educ_bot_bet avg_d_educ_bet avg_tage_bet_bot_bet avg_tage_bet_bet {

	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .
	
	eststo vcew`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcew`j'
		qui estadd scalar count_kreis = e(K2): vcew`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.15: Additional outcome variables, top panel
esttab vcew1 vcew2 vcew3 vcew4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcew5 vcew6 vcew7 vcew8, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.8: Table VII without matching

**	All firms
{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

//	Make sure that all counties in sample contain treated and control observations
egen group_strata = group(ao_kreis jahr)
bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.8 top left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

***	Firms with at least two establishments
{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

//	Make sure that all counties in sample contain treated and control observations
egen group_strata = group(ao_kreis jahr)
bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.8, top right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	BASELINE REGRESSION FOR FIRMS WITH ONE ESTABLISHMENT
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est == 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
		qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}

drop empl_perc*

egen group_strata = group(perc_empl jahr)
egen flg_strata = tag(perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
*** FIGURE VII, Table D.4, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, top left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est == 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Table D.9, top left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	BASELINE REGRESSION W/O INDIRECTLY AFFECTED ESTABLISHMENTS
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & indirect1 == 0

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
egen flg_strata = tag(perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Figure VII, Table D.4, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, top right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.11: Table D.4 SE clustered at firm x county level, top left
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & indirect1 == 0

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.9, top right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	ENTRY YEAR BEFORE 2000
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 2000

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Figure VII, Table D.4, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, middle left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.11: Table D.4 SE clustered at firm x county level, middle left
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 2000

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

egen group_strata = group(ao_kreis jahr)
//	Syntax ensures that only county x year cells w/ treated and control units included in obs. count

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.9, middle left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	ENTRY YEAR BEFORE 2000, MORE THAN ONE ESTABLISHMENT
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 2000 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Figure VII, Table D.4, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, middle right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.11: Table D.4 SE clustered at firm x county level, middle right
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 2000 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

egen group_strata = group(ao_kreis jahr)
//	Syntax ensures that only county x year cells with treated and control units included in obs. count

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )


cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.9, middle right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	ENTRY YEAR BEFORE 1995
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 1995

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Figure VII, Table D.4, bottom left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, bottom left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.11: Table D.4 SE clustered at firm x county level, bottom left
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 1995

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

egen group_strata = group(ao_kreis jahr)
//	Syntax ensures that only county x year cells w/ treated and control units included in obs. count

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.9, bottom left
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	ENTRY YEAR BEFORE 1995, MORE THAN ONE ESTABLISHMENT
*** Figure VII, Table D.4, D.7

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 1995 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Figure VII, Table D.4, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.7: Table D.4 unweighted, bottom right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.11: Table D.4 SE clustered at firm x county level, bottom right
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.9: Table D.4 w/o weights and w/o matching

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & entryyear < 1995 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

egen group_strata = group(ao_kreis jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcebet`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1, absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vcebet`j'
		qui estadd scalar count_kreis = e(K2): vcebet`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.9, bottom right
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcebet1 vcebet2 vcebet3 vcebet4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Stricter treatment definition: only consider connections without any changes
*** Table D.16 (unreported), full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1_direct == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1_direct == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1_direct if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1_direct == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1_direct == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1_direct if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1_direct == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1_direct == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1_direct == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1_direct == 1 & aux_touse == 1 & sample == 1

sum ever_faster1_direct if ever_faster1_direct == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1_direct if ever_faster1_direct == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1_direct == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1_direct == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1_direct if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1_direct if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Unreported: stricter definintion of treatment, full sample
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Stricter treatment definition: only consider connections without any changes
*** Table D.16, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1_direct == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1_direct == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1_direct if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1_direct == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1_direct == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1_direct if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1_direct == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1_direct == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1_direct == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1_direct == 1 & aux_touse == 1 & sample == 1

sum ever_faster1_direct if ever_faster1_direct == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1_direct if ever_faster1_direct == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1_direct == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1_direct == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1_direct if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1_direct if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.16, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Broader treatment definition: Assign to treatment group if closest establishment is treated
*** Table D.16 (unreported), full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

// add information 
merge m:1 untid ao_kreis using data/est_shortest_distance.dta
drop if _merge == 2
drop _merge
cap drop flg_est
egen flg_est = tag(betnr)
tab ptnr_ever_faster1 if flg_est == 1
tab ptnr_ever_faster1 if flg_est == 1 & ptnr_cty != hq_kreis
count

// Replace treatment dummy
replace faster1 = 1 if ever_faster1 == 0 & ptnr_ever_faster1 == 1 & ptnr_cty != hq_kreis & jahr >= ptnr_treat_year
replace ever_faster1 = 1 if ever_faster1 == 0 & ptnr_ever_faster1 == 1 & ptnr_cty != hq_kreis

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
*** Unreported: broader treatment definition, full sample
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Broader treatment definition: Assign to treatment group if closest establishment is treated
*** Table D.16, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

// add information 
merge m:1 untid ao_kreis using data/est_shortest_distance.dta
drop if _merge == 2
drop _merge
cap drop flg_est
egen flg_est = tag(betnr)
tab ptnr_ever_faster1 if flg_est == 1
tab ptnr_ever_faster1 if flg_est == 1 & ptnr_cty != hq_kreis
count

// Replace treatment dummy
replace faster1 = 1 if ever_faster1 == 0 & ptnr_ever_faster1 == 1 & ptnr_cty != hq_kreis & jahr >= ptnr_treat_year
replace ever_faster1 = 1 if ever_faster1 == 0 & ptnr_ever_faster1 == 1 & ptnr_cty != hq_kreis

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.16, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.18: Bundesland-year fixed effects, full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr bula_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr bula_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
*** Table D.18, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.18: Bundesland-year fixed effects, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr bula_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr bula_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Table D.18, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.19: Treatment dummy that excludes connection Hamburg-Berlin, full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1_rob == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1_rob == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1_rob if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1_rob == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1_rob == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1_rob if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1_rob == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1_rob == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1_rob == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1_rob == 1 & aux_touse == 1 & sample == 1

sum ever_faster1_rob if ever_faster1_rob == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1_rob if ever_faster1_rob == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1_rob == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1_rob == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1_rob if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1_rob if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.19, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Table D.19: Treatment dummy that excludes connection Hamburg-Berlin, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1_rob == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1_rob == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1_rob if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1_rob == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1_rob == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1_rob if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1_rob == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1_rob == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1_rob == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1_rob == 1 & aux_touse == 1 & sample == 1

sum ever_faster1_rob if ever_faster1_rob == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1_rob if ever_faster1_rob == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1_rob == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1_rob == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1_rob if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1_rob if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.19, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.20: Exclude largest establishments per county, full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

// find largest establishments
bys ao_kreis jahr: egen max_est_cty = max(empl_bet)
drop if empl_bet == max_est_cty

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.20, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.20: Exclude largest establishments per county, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

// find largest establishments
bys ao_kreis jahr: egen max_est_cty = max(empl_bet)
drop if empl_bet == max_est_cty

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.20, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.21: Exclude stations along the high-speed routes, full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1

drop if inlist(ao_kreis, 6533, 7143, 5382, 9161, 15091, 12070, 13076)

// 6533 - Limburg Sued, 7143 - Montabaur, 5382 - Siegburg/Bonn,
// 9161 - Ingolstadt
// 15091 - Lutherstadt Wittenberg
// 12070 - Wittenberge, 13076 - Ludwigslust

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.21, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.21: Exclude stations along the high-speed routes, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

drop if inlist(ao_kreis, 6533, 7143, 5382, 9161, 15091, 12070, 13076)

// 6533 - Limburg Sued, 7143 - Montabaur, 5382 - Siegburg/Bonn,
// 9161 - Ingolstadt
// 15091 - Lutherstadt Wittenberg
// 12070 - Wittenberge, 13076 - Ludwigslust

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.21, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.22: Drop moving establishments / headquarters, full sample

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & issue_est == 0 & issue_est_haupt == 0

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Table D.22, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}


********************************************************************************
*** Table D.22: Drop moving establishments / headquarters, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & issue_est == 0 & issue_est_haupt == 0 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
*** Table D.22, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.23: All years ICE connected, full sample

merge m:1 ao_kreis using data/AlwaysICE_ao.dta, keepusing(stetsICE)
drop _merge
replace stetsICE = 1 if ao_kreis == 2000 // Berlin

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & stetsICE == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.23, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore 
}

********************************************************************************
***	Table D.23: All years ICE connected, firms with more than one establishment

{
preserve
keep if hauptbet == 0 & d_traindata == 1 & stetsICE == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
	
	local j = `j' + 1
}

********************************************************************************
***	Table D.23, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore 
}


********************************************************************************
***	Table D.24: Split sample by predictability, full sample

//	Above-median predictability (= below-median problem arrival rate)
{
preserve
	
count
count if flg_est == 1

keep if hauptbet == 0 & d_traindata == 1

cap drop first_year
bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

cap drop flg_br1
egen flg_br1 = tag(br1)
count if flg_br1 == 1
qui sum mean_Dp_R_25 if flg_br1 == 1 & mean_Dp_R_25 != 0, d
cap drop touse
qui gen touse = (mean_Dp_R_25 <= r(p50) & mean_Dp_R_25 != 0)
count if touse == 1
	
keep if touse == 1
drop touse

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.24, top left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore	
}

//	Below-median predictability (= above-median problem arrival rate)
{
preserve

count
count if flg_est == 1

keep if hauptbet == 0 & d_traindata == 1
cap drop first_year
bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

cap drop flg_br1
egen flg_br1 = tag(br1)
count if flg_br1 == 1
qui sum mean_Dp_R_25 if flg_br1 == 1 & mean_Dp_R_25 != 0, d
cap drop touse
qui gen touse = (mean_Dp_R_25 > r(p50) & mean_Dp_R_25 != 0)
count if touse == 1
keep if touse == 1
drop touse

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}

qui gen perc_empl = 100 if empl_2000 < .
	forval q = 75(-25)25 {
		qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
egen flg_strata = tag(ao_kreis perc_empl jahr)

bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

tabstat tmp* if flg_strata == 1, c(s) s(N mean sd min p10 q p90 max)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
tabstat share_treated if flg_strata == 1, c(s) s(N mean q)
drop n_all_s share_treated

//	Within strata: # treated == # control
gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

//	Overall: sum of weights = # of observations
tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
cap drop flg_strata
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
	
local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.24, top right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
		
restore	
}

log close
